﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BCGC.BLL.Helper;
using Jalal.Library.Utility;
using BCGC.BLL.BusinessObject;
using System.Collections.Specialized;
using Jalal.Library.Mail;
using log4net;

public partial class Contact : PublicBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Submit_Click(object sender, ImageClickEventArgs e)
    {
        if (IsValid)
        {
            if (BCGC.BLL.BusinessObject.Contact.Create(Security.BindStringInput(tbxName.Text, SecurityLevel.Three),
                Security.BindStringInput(tbxOrganization.Text, SecurityLevel.Three),
                Security.BindStringInput(tbxEmail.Text, SecurityLevel.Three),
                Security.BindStringInput(tbxPhone.Text, SecurityLevel.Three),
                Security.BindStringInput(tbxWebsite.Text, SecurityLevel.Three),
                Security.BindStringInput(tbxSubject.Text, SecurityLevel.Three),
                Security.BindStringInput(tbxDescription.Text, SecurityLevel.Three),
                DateTime.Now, false))
            {
                uccResponse.SetSuccessMessage("Your contact information is accepted. Thank you.");

                try
                {
                    string body = tbxName.Text + "( " + tbxEmail.Text + " ) has contacted, and placed the following message\n\n";
                    NameValueCollection items = new NameValueCollection();
                    items.Add("Subject", "[Someone placed a contact request] : " + tbxSubject.Text);
                    items.Add("ReceiverName", Server.HtmlDecode(Settings.SiteSettings.CompanyEmail));
                    items.Add("ReceiverEmail", Server.HtmlDecode(Settings.SiteSettings.CompanyEmail));
                    MailSender.SendTextMaill(Settings.MailSettings, body + tbxDescription.Text, items);
                }
                catch(Exception ex)
                {
                    // Write the log into handled exception list
                    ILog log = LogHelper.GetHandledFileLogger("Contact");
                    if (log.IsErrorEnabled)
                        log.Error("Failed to send contact email", ex);
                }
            }
            else
                uccResponse.SetFailMessage("Failed to submit your message, please try again.");
        }
    }
}
